using Aliphant.Mvvm.XEvent.Interface;
using Aliphant.Mvvm.XEvent.Models;
using Aliphant.Mvvm.XEvent.UI;
using GrpcClient.Managers;
using GrpcClient.Models;
using System.Windows;

namespace GrpcClient.ViewModels
{
    public class SettingControlViewModel : BindableBase, INavigate
    {


        private int _theme;
        public int Theme { get => _theme; set => SetProperty(ref _theme, value, SetTheme); }

        private bool _openRegister;
        public bool OpenRegister { get => _openRegister; set => SetProperty(ref _openRegister, value, SaveRegisterFlag); }

        private Visibility _adminVisible;
        public Visibility AdminVisible { get => _adminVisible; set => SetProperty(ref _adminVisible, value); }

        private void SetTheme()
        {
            ThemeType theme = (ThemeType)Theme;
            ResourceManager.Instance.SetTheme(theme);
        }

        public void OnNavigateTo(NavigateParameter navigateParameter)
        {
            var theme = ResourceManager.Instance.CurrentTheme;
            Theme = (int)theme;

            if (ConfigContainer.Instance.cacheManager.Get("register", out bool isregister))
            {
                OpenRegister = isregister;
            }

            if (UserManager.Instance.CurrentUser?.Level == 0)
            {
                AdminVisible = Visibility.Visible;
            }
            else
            {
                AdminVisible = Visibility.Collapsed;
            }
        }

        private void SaveRegisterFlag()
        {
            ConfigContainer.Instance.cacheManager.Set("register", _openRegister);
        }
    }
}
